import React, { useState } from 'react';
import { Modal, Form, Input, Button, message } from 'antd';
import { updatePasswordUsingPut } from '@/services/api-share-backhend/userController';

const ModifyPasswordModal: React.FC = ({ showModify, onClose ,getNewLevel}) => {
  const [form] = Form.useForm();

  const updatePassword = async (values) => {
    try {
      const res = await updatePasswordUsingPut({ ...values });
      if (res.data !== null) {
        message.info('修改成功');
        getNewLevel(res.data)
        form.resetFields()
        onClose(); // 关闭对话框
      } else {
        message.error(res.message);
      }
    } catch (e) {
        console.log(e)
      message.error('服务器异常');
    }
  };
  const handleOk = () => {
    form
      .validateFields()
      .then((values) => {
        updatePassword(values);
      })
      .catch((e) => {
        console.log(e);
      });
  };

  return (
    <Modal
      title="修改密码"
      open={showModify}
      onOk={handleOk}
      onCancel={onClose}
      width={350}
      footer={[
        <Button key="back" onClick={onClose}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={handleOk}>
          确认
        </Button>,
      ]}
    >
      <Form form={form} layout="vertical" name="modify_password_form">
        <Form.Item
          name="userPassword"
          label="当前密码"
          rules={[
            { required: true, message: '请输入当前密码!' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (value && /\s/.test(value)) {
                  return Promise.reject(new Error('密码不能包含空格!'));
                }
                return Promise.resolve();
              },
            }),
          ]}
        >
          <Input.Password />
        </Form.Item>
        <Form.Item
          name="newUserPassword"
          label="新密码"
          type={'string'}
          rules={[
            { required: true, message: '请输入新密码!' },
            { min: 8, message: '密码长度不能少于8个字符!' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (value && /\s/.test(value)) {
                  return Promise.reject(new Error('密码不能包含空格!'));
                }
                return Promise.resolve();
              },
            }),
          ]}
        >
          <Input.Password />
        </Form.Item>
        <Form.Item
          name="secondNewUserPassword"
          label="确认新密码"
          dependencies={['newUserPassword']}
          rules={[
            { required: true, message: '请确认新密码!' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('newUserPassword') === value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('两次输入的密码不匹配!'));
              },
            }),
          ]}
        >
          <Input.Password />
        </Form.Item>
      </Form>
    </Modal>
  );
};


export default ModifyPasswordModal
